package com.fg.cloud.manager.service.factory.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSON;
import com.fg.cloud.common.PageUtils;
import com.fg.cloud.common.construction.ResultMap;
import com.fg.cloud.manager.entry.BaseEntity;
import com.fg.cloud.manager.entry.factory.FactoryAddressEntity;
import com.fg.cloud.manager.entry.factory.vo.FactoryAddressVo;
import com.fg.cloud.manager.entry.factory.vo.FactoryUserVo;
import com.fg.cloud.manager.mapper.BaseMapper;
import com.fg.cloud.manager.mapper.factory.FactoryAddressMapper;
import com.fg.cloud.manager.service.BaseServiceImpl;
import com.fg.cloud.manager.service.factory.FactoryAddressService;

@Service
public class FactoryAddressServiceImpl extends BaseServiceImpl implements FactoryAddressService {
	
	@Autowired
	private FactoryAddressMapper facAddressMapper;
	
	@Override
	protected <T extends BaseEntity> BaseMapper getBaseMapper() {
		return facAddressMapper;
	}
	
	/**
	 * 判重
	 */
	@Override
	public boolean judgeIsExit(FactoryAddressEntity address) {
		
		return facAddressMapper.judgeIsExit(address) > 0 ? true : false;
	}
	
	/**
	 * 编辑判重
	 */
	@Override
	public boolean JudgeIsExit(FactoryAddressEntity address) {
		
		return facAddressMapper.JudgeIsExit(address) > 0 ? true : false;
	}
	
	/**
	 * 验证ID合法
	 */
	@Override
	public boolean validateId(Long id, Long facId) {
		
		return facAddressMapper.validateId(id, facId) > 0 ? true : false;
	}
	
	/**
	 * 新增厂商收货地址
	 */
	@Override
	@Transactional
	public boolean insertAdd(FactoryAddressEntity address, FactoryUserVo user) {
		//处理省市区
		if(StringUtils.isNotEmpty(address.getAdministrative())) {
			if(address.getAdministrative().length() > 0) {
				List<ResultMap> array = JSON.parseArray(address.getAdministrative(), 
						ResultMap.class);
				if (array.size() == 3)
					address.setCity(array.get(1).getLabel());
				else
					address.setCity(array.get(0).getLabel());
				address.setArea(array.get(array.size()-1).getLabel());
				address.setProvince(array.get(0).getLabel());
			}
		}
		//处理默认收货地址
		if ( null == address.getIsDefault() ) address.setIsDefault(0);
		if ( 1 == address.getIsDefault() ) facAddressMapper.resetDefault(address.getFacId());
		address.setCreator(user.getId());
		address.setUpdater(user.getId());
		int flag = facAddressMapper.insertSelective(address);
		
		return flag > 0 ? true : false;
	}
	
	/**
	 * 编辑厂商收货地址
	 */
	@Override
	@Transactional
	public boolean editor(FactoryAddressEntity address, FactoryUserVo user, FactoryAddressVo find) {
		//处理省市区
		if(StringUtils.isNotEmpty(address.getAdministrative())) {
			if(address.getAdministrative().length() > 0) {
				List<ResultMap> array = JSON.parseArray(address.getAdministrative(), 
						ResultMap.class);
				if (array.size() == 3)
					address.setCity(array.get(1).getLabel());
				else
					address.setCity(array.get(0).getLabel());
				address.setArea(array.get(array.size()-1).getLabel());
				address.setProvince(array.get(0).getLabel());
			}
		}
		address.setFacId(user.getFacId());
		//处理默认地址
		if ( null == address.getIsDefault() ) address.setIsDefault(find.getIsDefault());
		if ( 1 == address.getIsDefault() ) facAddressMapper.resetDefault(address.getFacId());
		address.setUpdater(user.getId());
		
		boolean flag = updateByPrimaryKeySelective(address);
		return flag;
	}
	
	/**
	 * Description:设置默认地址
	 * @param id
	 * @param facId
	 * @return
	 * @author dingjiang
	 * @date 2017年9月27日下午4:38:54
	 */
	@Override
	@Transactional
	public boolean setDefault(Long id, FactoryUserVo user) {
		FactoryAddressEntity address = new FactoryAddressEntity();
		facAddressMapper.resetDefault(user.getFacId());
		address.setIsDefault(1);
		address.setId(id);
		address.setFacId(user.getFacId());
		address.setUpdater(user.getId());
		
		boolean flag = updateByPrimaryKeySelective(address);
		return flag;
	}

	/**
	 * 分页查询
	 */
	@Override
	public void selectPage(PageUtils<FactoryAddressVo> pageinfo) {
		long total = facAddressMapper.filterCount(pageinfo.getCondition());
		pageinfo.setTotal(total);
		if ( total > 0 ) {
			pageinfo.setData(facAddressMapper.findList(pageinfo));
		} else {
			pageinfo.setData(new ArrayList<FactoryAddressVo>());
		}	
	}
	
	@Override
	public List<FactoryAddressVo> selectAll(Long facId) {
		return facAddressMapper.selectAll(facId);
	}

	/**
	 * 批量删除
	 */
	@Override
	@Transactional
	public boolean deletes(Long facId, Long[] arrays) {
		
		return facAddressMapper.deletes(facId, arrays) > 0 ? true : false;
	}

	/**
	 * 禁用地址
	 */
	@Override
	public boolean disable(Long facId, Long id) {
		Byte status = 0;
		return facAddressMapper.modifyStatus(facId, status, id) > 0 ? true : false;
	}

	

	
}
